import { defineStore } from "pinia";
import { store } from "../index";
import Taro from "@tarojs/taro";

export const useMovieStore = defineStore("movie", {
  state: () => ({
    searchHistoryList:
      Taro.getStorageSync("store-movie-searchHistoryList") || [],
    cinemaLineList: Taro.getStorageSync("store-cinema-LineList") || [],
  }),
  getters: {},
  actions: {
    add(value) {
      let newList = this.searchHistoryList;
      newList.slice(0, 10);
      newList = newList.filter((item, index) => {
        return item.name !== value && index < 10;
      });
      newList.unshift({ name: value });
      this.searchHistoryList = newList;
      Taro.setStorageSync("store-movie-searchHistoryList", newList);
    },
    //保存院线列表
    savedList(val) {
      Taro.setStorageSync("store-cinema-LineList", val);
    },
    clear() {
      this.searchHistoryList = [];
      Taro.setStorageSync("store-movie-searchHistoryList", []);
    },
  },
});

export function getMovieStore() {
  return useMovieStore(store);
}
